package com.newegg.app.qec.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioGroup;

import com.newegg.app.qec.R;
import com.newegg.app.qec.fragment.CarFragment;
import com.newegg.app.qec.fragment.CategoryFragment;
import com.newegg.app.qec.fragment.HomeFragment;
import com.newegg.app.qec.fragment.MineFragment;
import com.newegg.app.qec.util.AndroidBug54971Workaround;
import com.newegg.app.qec.util.BaseActivity;
import com.newegg.app.qec.util.Constants;
import com.newegg.app.qec.view.MyRadioButton;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup tab_radioGroup;
    private HomeFragment homeFragment;
    private CategoryFragment categoryFragment;
    private CarFragment carFragment;
    private MineFragment mineFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private Fragment mContent;
    //底部栏数量
    private static final int tabNumber = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tab_radioGroup = (RadioGroup) findViewById(R.id.main_rg);
        tab_radioGroup.setOnCheckedChangeListener(this);


        homeFragment = new HomeFragment();
        categoryFragment = new CategoryFragment();
        carFragment = new CarFragment();
        mineFragment = new MineFragment();
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.main_frame, homeFragment).commit();
        mContent = homeFragment;

        //更新数据广播
        IntentFilter filter = new IntentFilter("QEC");
        registerReceiver(broadcastReceiver, filter);

    }

    public void switchContent(Fragment to) {
        //如果需要展示的fragment不是当前fragment
        if (mContent != to) {
            transaction = fragmentManager.beginTransaction();
            //如果需要打开的fragment没有存在，先创建在显示
            if (!to.isAdded()) {
                transaction.hide(mContent);
                transaction.add(R.id.main_frame, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
                mContent = to;
            } else {
                //如果需要打开的fragment存在，就显示
                transaction.hide(mContent);
                transaction.show(to).commit(); // 隐藏当前的fragment，显示下一个
                mContent = to;
            }
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int i = 0; i < tabNumber; i++) {
            MyRadioButton tabrb = (MyRadioButton) tab_radioGroup.getChildAt(i);
            if (tabrb.isChecked()) {
                tabrb.setTextColor(getResources().getColor(R.color.red));
            } else {
                tabrb.setTextColor(getResources().getColor(R.color.ziti_color));
            }
        }
        switch (checkedId) {
            case R.id.navigation_bar_home:
                switchContent(homeFragment);
                break;
            case R.id.navigation_bar_category:
                switchContent(categoryFragment);
                break;
            case R.id.navigation_bar_car:
                switchContent(carFragment);
                break;
            case R.id.navigation_bar_mine:
                switchContent(mineFragment);
                break;
        }
    }

    /**
     * 信息修改后返回
     */
    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            int verification = intent.getIntExtra("QEC", -1);
            //0  获取数据    1 取消数据
            if (!TextUtils.isEmpty(action)) {
                //更新个人信息,在登录处发送广播
                mineFragment.MineFragment(verification);
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(broadcastReceiver);
    }
}
